﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DAO
{
    public class Reservation
    {
        private DateTime startDate;

        public DateTime StartDate
        {
            get { return startDate; }

        }
        private DateTime endDate;

        public DateTime EndDate
        {
            get { return endDate; }

        }

        public static void InsertReservation(string FirstName, string LastName, string Phone, string Email, DateTime StartDate, DateTime EndDate, string RoomNumber, string Status, string Egn)
        {

            DataSetReserveTableAdapters.ClientsTableAdapter taClients =
                   new DAO.DataSetReserveTableAdapters.ClientsTableAdapter();
            DataSetReserveTableAdapters.ClientsTableAdapter taClientID =
                new DAO.DataSetReserveTableAdapters.ClientsTableAdapter();
            DataSetReserve.ClientsDataTable dtClientID =
                taClients.GetDataByEGN(Egn);
            int clientID=0;
            if(dtClientID.Count!=0){
            clientID = dtClientID[0].ClientID;
            }
            if(clientID==0)
            {
                clientID=taClients.InsertClients(FirstName, LastName, Phone, Egn);
            }

            DataSetReserveTableAdapters.ReseravtionStatusTableAdapter taReservationStatus =
                new DAO.DataSetReserveTableAdapters.ReseravtionStatusTableAdapter();
            DataSetReserve.ReseravtionStatusDataTable dtReservationTable =
                taReservationStatus.GetStatusIDByName(Status);
            int statusID = dtReservationTable[0].StatusID;

            DataSetReserveTableAdapters.RoomsTableAdapter taRooms =
                new DAO.DataSetReserveTableAdapters.RoomsTableAdapter();
            Room room = DAO.Room.GetByNumber(RoomNumber);
            int roomID = room.Id;

            DataSetReserveTableAdapters.ReservationTableAdapter taReservation =
                new DAO.DataSetReserveTableAdapters.ReservationTableAdapter();
            taReservation.InsertReservation(roomID, clientID, statusID, StartDate, EndDate);
        }

        public static Reservation[] GetReservations(string Egn)
        {
            DataSetReserveTableAdapters.ReservationTableAdapter taReservation =
                new DAO.DataSetReserveTableAdapters.ReservationTableAdapter();
            DataSetReserve.ReservationDataTable dtReservation =
                taReservation.GetDataByEGN(Egn);

            DataSetReserveTableAdapters.ClientsTableAdapter taClients =
                new DAO.DataSetReserveTableAdapters.ClientsTableAdapter();
            DataSetReserve.ClientsDataTable dtClients = taClients.GetDataByEGN(Egn);

            DataSetReserveTableAdapters.RoomsTableAdapter taRoom =
                new DAO.DataSetReserveTableAdapters.RoomsTableAdapter();
            DataSetReserve.RoomsDataTable dtRoom = taRoom.GetRooms();

            DataSetReserveTableAdapters.ReseravtionStatusTableAdapter taStatusReservation =
                new DAO.DataSetReserveTableAdapters.ReseravtionStatusTableAdapter();
            DataSetReserve.ReseravtionStatusDataTable dtStatusReservation =
                taStatusReservation.GetReservationStatus();

            List<Reservation> listOfReservations = new List<Reservation>();

            for (int i = 0; i < dtReservation.Rows.Count; i++)
            {
                Reservation myReservation = new Reservation();

                myReservation.startDate = dtReservation[i].StartDate;
                myReservation.endDate = dtReservation[i].EndDate;


                myReservation.clientFirstName = dtClients[i].FirstName;
                myReservation.clientLastName = dtClients[i].LastName;
                myReservation.clientEgn = dtClients[i].Egn;
                myReservation.clientPhone = dtClients[i].Phone;
                myReservation.roomNumber = dtRoom[i].Number;
                Room current = Room.GetByNumber(dtRoom[0].Number);
                myReservation.room = current;
                myReservation.prise = dtRoom[i].Prise;
                myReservation.status = dtStatusReservation[i].Name;

                listOfReservations.Add(myReservation);
            }

            return listOfReservations.ToArray();
        }
        /*
        public static Reservation[] GetReservations(DateTime date)
        {

            DataSetReserveTableAdapters.ReservationTableAdapter taReservation =
                new DAO.DataSetReserveTableAdapters.ReservationTableAdapter();
            DataSetReserve.ReservationDataTable dtReservation =
                taReservation.GetDataByDate(date);

            DataSetReserveTableAdapters.ClientsTableAdapter taClients =
                new DAO.DataSetReserveTableAdapters.ClientsTableAdapter();
            DataSetReserve.ClientsDataTable dtClients = taClients.GetClients();

            DataSetReserveTableAdapters.RoomsTableAdapter taRoom =
                new DAO.DataSetReserveTableAdapters.RoomsTableAdapter();
            DataSetReserve.RoomsDataTable dtRoom = taRoom.GetRooms();

            DataSetReserveTableAdapters.ReseravtionStatusTableAdapter taStatusReservation =
                new DAO.DataSetReserveTableAdapters.ReseravtionStatusTableAdapter();
            DataSetReserve.ReseravtionStatusDataTable dtStatusReservation =
                taStatusReservation.GetReservationStatus();

            List<Reservation> listOfReservations = new List<Reservation>();

            for (int i = 0; i < dtReservation.Rows.Count; i++)
            {
                Reservation myReservation = new Reservation();

                myReservation.startDate = dtReservation[i].StartDate;
                myReservation.endDate = dtReservation[i].EndDate;
                myReservation.clientFirstName = dtClients[i].FirstName;
                myReservation.clientLastName = dtClients[i].LastName;
                myReservation.clientEgn = dtClients[i].Egn;
                myReservation.clientPhone = dtClients[i].Phone;
                myReservation.roomNumber = dtRoom[i].Number;
                Room current = Room.GetByNumber(dtRoom[0].Number);
                myReservation.room = current;
                myReservation.prise = dtRoom[i].Prise;
                myReservation.status = dtStatusReservation[i].Name;

                listOfReservations.Add(myReservation);
            }

            return listOfReservations.ToArray();
        }
         * */

        public static Reservation[] GetReservations(string FirstName, string LastName)
        {

            DataSetReserveTableAdapters.ReservationTableAdapter taReservation =
                new DAO.DataSetReserveTableAdapters.ReservationTableAdapter();
            DataSetReserve.ReservationDataTable dtReservation =
                taReservation.GetDataByFirstNameLastName(FirstName, LastName);

            DataSetReserveTableAdapters.ClientsTableAdapter taClients =
                new DAO.DataSetReserveTableAdapters.ClientsTableAdapter();
            DataSetReserve.ClientsDataTable dtClients = taClients.GetClients();

            DataSetReserveTableAdapters.RoomsTableAdapter taRoom =
                new DAO.DataSetReserveTableAdapters.RoomsTableAdapter();
            DataSetReserve.RoomsDataTable dtRoom = taRoom.GetRooms();

            DataSetReserveTableAdapters.ReseravtionStatusTableAdapter taStatusReservation =
                new DAO.DataSetReserveTableAdapters.ReseravtionStatusTableAdapter();
            DataSetReserve.ReseravtionStatusDataTable dtStatusReservation =
                taStatusReservation.GetReservationStatus();

            List<Reservation> listOfReservations = new List<Reservation>();

            for (int i = 0; i < dtReservation.Rows.Count; i++)
            {
                Reservation myReservation = new Reservation();

                myReservation.startDate = dtReservation[i].StartDate;
                myReservation.endDate = dtReservation[i].EndDate;
                myReservation.clientFirstName = dtClients[i].FirstName;
                myReservation.clientLastName = dtClients[i].LastName;
                myReservation.clientEgn = dtClients[i].Egn;
                myReservation.clientPhone = dtClients[i].Phone;
                myReservation.roomNumber = dtRoom[i].Number;
                Room current = Room.GetByNumber(dtRoom[0].Number);
                myReservation.room = current;
                myReservation.prise = dtRoom[i].Prise;
                myReservation.status = dtStatusReservation[i].Name;

                listOfReservations.Add(myReservation);
            }

            return listOfReservations.ToArray();
        }

        public static Reservation GetReservation(string RoomNumber)
        {
            DataSetReserveTableAdapters.ReservationTableAdapter taReservation =
               new DAO.DataSetReserveTableAdapters.ReservationTableAdapter();
            DataSetReserve.ReservationDataTable dtReservation =
                taReservation.GetDataByRoomNumber(RoomNumber);

            DataSetReserveTableAdapters.ClientsTableAdapter taClients =
                new DAO.DataSetReserveTableAdapters.ClientsTableAdapter();
            DataSetReserve.ClientsDataTable dtClients = taClients.GetClients();

            DataSetReserveTableAdapters.RoomsTableAdapter taRoom =
                new DAO.DataSetReserveTableAdapters.RoomsTableAdapter();
            DataSetReserve.RoomsDataTable dtRoom = taRoom.GetRooms();

            DataSetReserveTableAdapters.ReseravtionStatusTableAdapter taStatusReservation =
                new DAO.DataSetReserveTableAdapters.ReseravtionStatusTableAdapter();
            DataSetReserve.ReseravtionStatusDataTable dtStatusReservation =
                taStatusReservation.GetReservationStatus();

            Reservation myReservation = new Reservation();

            myReservation.startDate = dtReservation[0].StartDate;
            myReservation.endDate = dtReservation[0].EndDate;
            myReservation.clientFirstName = dtClients[0].FirstName;
            myReservation.clientLastName = dtClients[0].LastName;
            myReservation.clientEgn = dtClients[0].Egn;
            myReservation.clientPhone = dtClients[0].Phone;
            myReservation.roomNumber = dtRoom[0].Number;
            Room current = Room.GetByNumber(dtRoom[0].Number);
            myReservation.room = current;
            myReservation.prise = dtRoom[0].Prise;
            myReservation.status = dtStatusReservation[0].Name;

            return myReservation;
        }

        public void ChangeStatus(string RoomNumber, string Status)
        {
            DataSetReserveTableAdapters.ReservationTableAdapter taReservation =
               new DAO.DataSetReserveTableAdapters.ReservationTableAdapter();
            DataSetReserve.ReservationDataTable dtReservation =
                taReservation.GetDataByRoomNumber(RoomNumber);
            int reservationStatusID = dtReservation[0].StatusID;
            DataSetReserveTableAdapters.ReseravtionStatusTableAdapter taStatus =
               new DAO.DataSetReserveTableAdapters.ReseravtionStatusTableAdapter();
            DataSetReserve.ReseravtionStatusDataTable resStatus = taStatus.GetData();

            DataSetReserveTableAdapters.ReservationTableAdapter taReservationStatus =
                new DAO.DataSetReserveTableAdapters.ReservationTableAdapter();
            DataSetReserve.ReseravtionStatusDataTable result = taStatus.GetStatusIDByName(Status);
            int key = result[0].StatusID;
            taReservationStatus.UpdateReservationStatus(
                                                    dtReservation[0].RoomID,
                                                    dtReservation[0].ClientID,
                                                    key,
                                                    dtReservation[0].StartDate,
                                                    dtReservation[0].EndDate,
                                                    dtReservation[0].ReservationID,
                                                    dtReservation[0].RoomID,
                                                    dtReservation[0].ClientID,
                                                    dtReservation[0].StatusID,
                                                    dtReservation[0].StartDate,
                                                    dtReservation[0].EndDate,
                                                    dtReservation[0].ReservationID);


        }

        public void DeleteReservation(string RoomNumber)
        {
            DataSetReserveTableAdapters.ReservationTableAdapter taReservation =
                new DAO.DataSetReserveTableAdapters.ReservationTableAdapter();
            taReservation.DeleteReservationByRoomNumber(RoomNumber);
        }

        private string clientFirstName;

        public string ClientFirstName
        {
            get { return clientFirstName; }

        }
        private string clientLastName;

        public string ClientLastName
        {
            get { return clientLastName; }

        }
        private string clientPhone;

        public string ClientPhone
        {
            get { return clientPhone; }

        }
        private string clientEgn;

        public string ClientEgn
        {
            get { return clientEgn; }

        }

        private string status;

        public string Status
        {
            get { return status; }
            set { status = value; }
        }
        private Room room;

        private string roomNumber;
        public string RoomNumber
        {
            get
            {
                return room.Number;
            }

        }

        public string RoomType
        {
            get
            {
                return room.RoomType;
            }


        }
        private decimal prise;
        public decimal Prise
        {
            get
            {
                return prise;

            }

        }


    }
}
